//
//  SearchProjectViewController.m
//  OCPB
//
//  Created by Siroj Chongkolwanont on 7/5/56 BE.
//  Copyright (c) 2556 Siroj Chongkolwanont. All rights reserved.
//

#import "SearchProjectViewController.h"
#import "ComplainViewController.h"
#import "ProjectComplain.h"

@interface SearchProjectViewController ()

@end



@implementation SearchProjectViewController



id jsonObjectFromUrlString(NSString *urlString);

NSMutableArray *projectArray;

//@synthesize searchBar;
@synthesize tableView;
@synthesize complainController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (NSInteger)numberOfComponentsInPickerView:(UITableView *)tableView;
{
    // The number of sections in the UIPickerView
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger row = [projectArray count];
    
    return row;
}




- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ProjectCell";
    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSInteger row = indexPath.row;
    
    //News* hilight = hilightArray[row];
    ProjectComplain* project = projectArray[row];
    
    
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // Here we use the new provided setImageWithURL: method to load the web image
    
    UILabel *nameProject = (UILabel *)[cell viewWithTag:100];
    nameProject.text = project.project_name;

    
    return cell;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
    //self.theTableView.allowsSelection = NO;
    //self.theTableView.scrollEnabled = NO;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    searchBar.text=@"";
    
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
    //self.theTableView.allowsSelection = YES;
    //self.theTableView.scrollEnabled = YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    // You'll probably want to do this on another thread
    // SomeService is just a dummy class representing some
    // api that you are using to do the search
    //NSArray *results = [SomeService doSearch:searchBar.text];
	
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
    
    [self seachProject:searchBar.text];
    [tableView reloadData];
    
    

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = indexPath.row;
    
    //ComplainViewController* complainview =  (ComplainViewController*)self.navigationController.parentViewController;
    [complainController setProjectComplain:projectArray[row]];
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) seachProject:(NSString*)searchText
{
    projectArray = [[NSMutableArray alloc] init];
    
    
    NSString *url_str = [NSString stringWithFormat:@"http://www.aitproject.com/demo/webservice/public/service/person-complained-project?name=%@", searchText];
    
    NSDictionary *publicTimeline =  jsonObjectFromUrlString(url_str);
    
    
    for(NSString *item in [publicTimeline allKeys]) {
        
        if([item isEqual: @"data"] )
        {
            NSDictionary *allData = [publicTimeline valueForKey:item];
            
            NSArray *allSue = [allData valueForKey:item];
            
            for(NSDictionary *data in allSue )
            {
                ProjectComplain* project = [[ProjectComplain alloc] init];
                project.project_id = [data valueForKey:@"Person_Comp_Project_id"];
                project.project_name = [data valueForKey:@"Person_Comp_Project_Name"];
                
                [projectArray addObject:project];
                
            }
        }
    }
    
    //currentCause = causeTypeArray[0];
}
- (void) clearData
{
    [projectArray removeAllObjects];
    
    [tableView reloadData];
}

@end
